Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Datatypes

Python Bytes

In Python, bytes is an immutable sequence of integers in the range 0 <= x < 256. It’s a built-in type that has several methods for manipulation and it’s often used when dealing with binary data or byte-oriented protocols. You can create bytes in Python in several ways:

Using a string and specifying an encoding:

Creating bytes using a string and specifying an encoding b = bytes('Hello, world!', 'utf-8') print(b)

Output

b'Hello, world!'
In this case, ‘utf-8’ is the encoding. An encoding is a set of rules that determine how a string of characters is converted into bytes.

Using a list or tuple of integers:

Creating bytes using a list or tuple of integers b = bytes([65, 66, 67]) print(b)

Output

b'ABC'
Each integer should be in the range 0 <= x < 256.

Using the bytes() constructor with no arguments:

Creating bytes using the bytes() constructor with no arguments b = bytes() # This will create an empty bytes object print(b) print(type(b))

Output

b'' <class 'bytes'>
Once you have a bytes object, you can access individual elements just like you would with a list, but you can’t modify them because bytes are immutable:

Bytes are Immutable:

Example of bytes in python to understand immutable b = bytes('Hello, world!', 'utf-8') print(b[0]) # This will print 72, the ASCII value of 'H'

Output

72
Note : b[0] = 74 # This will raise a TypeError because bytes are immutable. You can also use most of the common sequence operations on bytes, like len(), slicing, and concatenation. len(): The len() function returns the number of elements in a bytes object.
Finding length of python bytes object using len() b = bytes('Hello, world!', 'utf-8') print(len(b))

Output

13
Slicing: You can use slicing to get a part of the bytes object. The syntax is b[start:stop], where start is the index to start from and stop is the index to end at.
slicing bytes in python with start and stop index b = bytes('Hello, world!', 'utf-8') print(b[0:5])

Output

b'Hello'
Concatenation: You can concatenate two bytes objects using the + operator.
bytes concatenation using + operator in python basic example b1 = bytes('Hello,', 'utf-8') b2 = bytes(' world!', 'utf-8') b = b1 + b2 print(b)

Output

b'Hello, world!'
Remember, bytes are immutable. This means that once a bytes object is created, it cannot be changed. So, when you do slicing or concatenation, a new bytes object is created.

Summary

Creation: You can create a bytes object from other data such as lists, strings etc. For example, if you have a list myList = [1,2,3,4,5], you can create a bytes object from this list as bytes_obj = bytes(myList). ⮚ Characteristics: Python bytes are a sequence of integers in the range of 0-255. They are an immutable sequence data type, meaning once a bytes object is created, it cannot be changed. ⮚ Usage: The bytes type is a sequence of bytes that have been encoded and are ready to be stored in memory/disk. There are many types of encodings (utf-8, utf-16, windows-1255), which all handle the bytes differently. The bytes object can be decoded into a str type. ⮚ Applications: Bytes are typically used when you encounter a string with a unique encoding, or when a library requires it. Other applications include reading/writing binary data, like image or audio, from a file or a web API. Remember, Python uses the bytes data type to improve the readability of code and make it consistent across the wide spectrum of Python code. Consistency within one module or function is the most important.

  📌TAGS

★python ★ datatypes ★ binary datatype ★ bytes

Tutorials